<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\Common\AjaxPage;
use AuctionCore\Other\Address;

/**
 * 换车管理
 *
 * @author Xmh
 */
class ChangeCarController extends BaseController
{

    private $changeCarLogic;

    private $empId;

    public function __construct()
    {
        parent::__construct();
        $this->changeCarLogic = D('ChangeCar', 'Logic');
        $this->empId = $this->user_info['emp_id'];
    }

    public function index()
    {
        $addr = new Address();
        $city_list = $addr->get_by_level(1);
        $this->assign('city_list', $city_list);
        $this->display();
    }

    /**
     * 换车列表
     */
    public function changeCarList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = [];
        $params['start_time'] = I('get.start_time', '');
        $params['end_time'] = I('get.end_time', '');
        $params['location_area'] = I('get.location_area', '');
        $params['status'] = I('get.status/d', 0);
        $result = $this->changeCarLogic->getChangeCarList($page, $size, $params);
        $list = !empty($result['list']) ? $result['list'] : [];
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();          // 分页显示输出
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $page);
        $this->assign('total', $result['total']);
        $this->assign('params', $params);
        $this->display();
    }

    /**
     * 换车详情
     */
    public function changeCarDetail()
    {
        $chgId = I('get.id/d', 0);
        if ($chgId < 1) {
            $this->redirect('ChangeCar/index', '', 1, '请求出错了');
        }
        $changeCarInfo = $this->changeCarLogic->getChangeCarInfo($chgId);
        if ($changeCarInfo['chg_status'] > 1) {
            $this->redirect('ChangeCar/index', '', 1, '置换状态错误');
        }
        $this->assign('detail', $changeCarInfo);
        $this->display();
    }

    /**
     * 换车处理
     */
    public function doChangeCar()
    {
        $chgId = I('post.id/d', 0);
        if ($chgId < 1) {
            $this->helpJsonResult('400', '请求数据错误');
        }
        $chg_status = I('post.chg_status/d', 0);
        if ($chg_status < 1) {
            $this->helpJsonResult('400', '置换状态有误');
        }
        $chgRemark = I('post.chg_remark', '', 'trim');
        if (empty($chgRemark)) {
            $this->helpJsonResult('400', '备注不能为空');
        }
        $data['id'] = $chgId;
        $data['chg_status'] = $chg_status;
        $data['remark'] = $chgRemark;
        $data['deal_time'] = time();
        $res = $this->changeCarLogic->updateChangeCar($data);
        if (!$res) {
            $this->helpJsonResult('400', '置换信息处理失败');
        }
        $this->helpJsonResult('200', '置换信息处理成功');
    }

    /**
     * 换车详情
     */
    public function changeCarInfo()
    {
        $chgId = I('get.id/d', 0);
        if ($chgId < 1) {
            $this->redirect('ChangeCar/index', '', 1, '请求出错了');
        }
        $changeCarInfo = $this->changeCarLogic->getChangeCarInfo($chgId);
        if ($changeCarInfo['chg_status'] < 2) {
            $this->redirect('ChangeCar/index', '', 1, '置换状态错误');
        }
        $this->assign('detail', $changeCarInfo);
        $this->display();
    }
}
